//
//  ConcentrationViewController.m
//  A手工客
//
//  Created by 萌萌的小坤坤 on 16/6/13.
//  Copyright © 2016年 冷坤. All rights reserved.
//

#import "ConcentrationViewController.h"
#import "HeadView.h"
#import "HeadOneCollectionView.h"
#import "SlideModel.h"
#import "NavigationModel.h"
#import "AdvanceModel.h"
#import "HotTopicModel.h"
#import "AFNetworking.h"
#import "HotTopicTabView.h"
#define SELECTNET @"http://m.shougongke.com/index.php?&c=index&a=indexNewest&vid=18"
#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

#define COLORS [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0];

@interface ConcentrationViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain) UITableView *tabView;
@property (nonatomic ,retain) HeadView *headView;
@property (nonatomic ,retain) HeadOneCollectionView *headOne;

@property (nonatomic ,retain) NSMutableArray *arrSlide;
@property (nonatomic ,retain) NSMutableArray *arrNavigation;
@property (nonatomic ,retain) NSMutableArray *arrAdvance;
@property (nonatomic ,retain) NSMutableArray *arrHotTopic;




@end

@implementation ConcentrationViewController
- (NSMutableArray *)arrSlide{
    if (!_arrSlide) {
        self.arrSlide = [NSMutableArray array];
    }
    return _arrSlide;
}
- (NSMutableArray *)arrNavigation{
    if (!_arrNavigation) {
        self.arrNavigation = [NSMutableArray array];
    }
    return _arrNavigation;
}
- (NSMutableArray *)arrAdvance{
    if (!_arrAdvance) {
        self.arrAdvance = [NSMutableArray array];
    }
    return _arrAdvance;
}
- (NSMutableArray *)arrHotTopic{
    if (!_arrHotTopic) {
        self.arrHotTopic = [NSMutableArray array];
    }
    return _arrHotTopic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getSelectUrl];
    [self creatTabView];
}

- (void)creatHeadView{
    
    self.headView = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 400) DiscoveryArray:self.arrNavigation RotateArray:self.arrSlide ImageArray:self.arrAdvance];
    
    self.tabView.tableHeaderView = self.headView;
    
    
}

- (void)creatTabView{
    self.tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100) style:1];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    [self.view addSubview:self.tabView];
    
}

# pragma mark - 网络请求
- (void)getSelectUrl{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    
    [manager GET:SELECTNET parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:
    ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicData = responseObject[@"data"];
        NSMutableArray *arrSlide_n = dicData[@"slide"];
        
        for (NSDictionary *dic in arrSlide_n) {
            SlideModel *slideModel = [SlideModel slideModelWithDic:dic];
            [self.arrSlide addObject:slideModel];
            
        }
        NSMutableArray *arrNavigation_n = dicData[@"navigation"];
        for (NSDictionary *dic in arrNavigation_n) {
            NavigationModel *navigationModel = [NavigationModel navigationModelWithDic:dic];
            [self.arrNavigation addObject:navigationModel];
        }
        
        NSMutableArray *arrAdvance_n = dicData[@"advance"];
        for (NSDictionary *dic in arrAdvance_n) {
            AdvanceModel *advanceModel = [AdvanceModel advanceModelWithDic:dic];
            [self.arrAdvance addObject:advanceModel];
        }
        
        NSMutableArray *arrHotTopic_n = dicData[@"hotTopic"];
        for (NSDictionary *dic in arrHotTopic_n) {
            HotTopicModel *hotTopicModel = [HotTopicModel hotTopicModelWithDic:dic];
            [self.arrHotTopic addObject:hotTopicModel];
        }
        
        [self creatHeadView];
        [self.tabView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrHotTopic.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    HotTopicTabView *tabViewCell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (tabViewCell == nil) {
        tabViewCell = [[HotTopicTabView alloc]initWithStyle:1 reuseIdentifier:ID];
    }
//    SlideModel *models = self.arrSlide[indexPath.row];
    HotTopicModel *models = self.arrHotTopic[indexPath.section];
    tabViewCell.model = models;
    tabViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return tabViewCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 667/3.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 25;
    }else{
        
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForheaderInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 20)];
    label.text = @"热门专题";
    label.textAlignment = 1;
    if (section == 0) {
        return label;
    }else{
        return nil;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
